package org.iteam.xlz.manager.utils;

import org.apache.struts2.json.JSONUtil;
import org.iteam.commons.utils.DateUtil;
import org.iteam.web.shiro.ShiroUtil;
import org.iteam.web.struts.action.AbstractModuleAction;
import org.iteam.xlz.core.entity.log.LogCreator;
import org.iteam.xlz.core.service.LogCreatorService;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LogInterceptor extends AbstractInterceptor{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7385326159108946206L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		String result=null;
		
		ActionProxy proxy =invocation.getProxy();
		String actionName=proxy.getActionName();
		if(actionName.equals("create")||actionName.equals("update")||actionName.equals("confirm")){
			Object o=invocation.getAction();
			AbstractModuleAction<?> a=(AbstractModuleAction<?>)o;
			String content=JSONUtil.serialize(a.getModel());
			String name=a.getModel().getClass().getName();
			String admin=ShiroUtil.currentAccount();
			LogCreator creator=new LogCreator();
			creator.setLogtype(actionName.equals("create")?3:4);
			creator.setCreator(admin);
			result=invocation.invoke();
			creator.setLogcontent("管理员"+admin+"在"+DateUtil.currentTime()+"对"+name+"对象进行了"+actionName+"操作,写库的字段有:"+content+",操作结果state="+a.map.get("state"));
			LogCreatorService.getInstance().create(creator);
		}else{
			return invocation.invoke();
		}
		return result;
	}

}
